import scrapy
from scrapy_movie_05.items import ScrapyMovie05Item

class MovieSpider(scrapy.Spider):
    name = "movie"
    allowed_domains = ["www.dydytt.net"]
    start_urls = ["https://www.dydytt.net/html/gndy/china/index.html"]

    def parse(self, response):
        # 要第一个的名字 和 第二个的图片
        # //div[@class="co_content8"]//table//a[2]
        a_list = response.xpath('//div[@class="co_content8"]//table//a[2]')
        for a in a_list:
            # 获取第一页的 name 和 要点击的链接
            name = a.xpath('./text()').extract_first()
            href = a.xpath('./@href').extract_first()

            url = f"https://www.dydytt.net{href}"
            yield scrapy.Request(url, callback=self.parse_detail, meta={"name": name})
        pass

    def parse_detail(self, response):
        src = response.xpath('//div[@id="Zoom"]//img/@src').extract_first()
        # 接受请求的那个 meta 参数的值
        name = response.meta.get("name")

        item = ScrapyMovie05Item(src=src, name=name)
        yield item
